package com.other.thread;

public class ThreadDemo2 {
	public static void main(String[] args) {
		for (int i = 0; i < 5; i++) {
			Thread t = new Thread(new All());
			
			t.start();
		}
	}
}

class All implements Runnable{
	
	@Override
	public void run() {
		Parent p = new Parent();
		Son s = new Son();
		Thread t1 = new Thread(p);
		Thread t2 = new Thread(s);
		s.keepRunning = true;
		t2.start();
		p.keepRunning = false;
		try {
			t2.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		p.keepRunning = true;
		t1.start();
		s.keepRunning = false;
		try {
			t1.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	
}
class Parent implements Runnable{
	public volatile static boolean keepRunning = true;
	private static final Object lock = new Object();
	@Override
	public void run() {
		synchronized (lock) {
			if(keepRunning){
				for (int i = 1; i <= 20; i++) {
					System.out.println("---主程序运行--"+i+"**************");
				}
				
			}
			
		}
	}
	
	
}
class Son implements Runnable{
	public volatile static boolean keepRunning = true;
	private static final Object locked = new Object();
	@Override
	public void run() {
		synchronized (locked) {
			if(keepRunning){
				for (int i = 1; i <= 10; i++) {
					System.out.println("---子程序运行--"+i);
				}
			}
		}
	}
}
